package com.probiz.estoresa.content.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.content.ContentRelatedType;
import com.probiz.estore.content.service.ContentRelatedTypeManager;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.core.model.BaseObject;

public class ContentRelatedTypeController extends GenericStoreAdminController<ContentRelatedType> {
    private ContentRelatedTypeManager contentRelatedTypeManager = null;

    public void setContentRelatedTypeManager(ContentRelatedTypeManager inMgr) {
        this.contentRelatedTypeManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(ContentRelatedType entity) {
		return entity.getContentRelatedTypeName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = contentRelatedTypeManager;
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("0", "convert_gbk(relatedTitle)");
		sortMap.put("1", "convert_gbk(typeName)");
		sortMap.put("2", "ruleCode");
		sortMap.put("3", "isApplyToContent");
		sortMap.put("4", "autoEval");
		sortMap.put("5", "status*-1");
		return sortMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#onSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, ContentRelatedType entity, BindException errors) {
	}
	
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 取得Form对应的Model
		ContentRelatedType entity = formBackingObject(request);

		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, entity);
			errors = new BindException(binder.getBindingResult());
			// 传给子类的实现，后者可以继续验证和加入错误到errors
			preSave(request, entity, errors);
			if (!errors.hasErrors()) {// 里面如果出错应该抛出异常
				if(entity.getContentRelatedTypeId() == null){
					entity.setIsSystem(Constants.FLAG_TRUE);
				}
				mgr.save(entity);
				String msgKey = (isEntityNew(request)) ? "common.added"
						: "common.updated";
				String message = getMessage(msgKey, new Object[] {
						getEntityTypeMessage(), getEntityName(entity) });
				saveMessage(request, message);
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}

		ModelAndView mav;
		if (errors.hasErrors()) {
			mav = showForm(request, errors);
		} else if (successView != null) {
			mav = getModelAndView(successView, errors.getModel());
		} else {
			mav = getRedirectToActionView("edit", ((BaseObject) entity).getId()
					.toString());
		}
		return mav;
	}

}
